package com.jiangtai.training.ui.splash

import android.Manifest
import android.content.Intent
import android.util.Log
import android.widget.Toast
import com.bumptech.glide.Glide
import com.cld.mapapi.map.CldMap
import com.cld.navisdk.CldNaviManager
import com.cld.utils.CldTask
import com.jaeger.library.StatusBarUtil
import com.jiangtai.training.R
import com.jiangtai.training.base.BaseActivity
import com.jiangtai.training.glide.GlideUtils
import com.jiangtai.training.ui.main.MainActivity
import com.jiangtai.training.util.KeyUtil
import com.orhanobut.logger.Logger
import kotlinx.android.synthetic.main.activity_splash.*
import org.litepal.LitePal
import showToast

/**
 * Created by heCunCun on 2021/3/8
 */
class SplashActivity : BaseActivity() {
    private val PERMISS_REQUEST_CODE = 0x100
    override fun attachLayoutRes(): Int = R.layout.activity_splash

    override fun initData() {
       Glide.with(this).load(R.drawable.splash).into(iv_splash)
    }

    override fun initView() {
        //请求权限
        if (checkPermissions(
                arrayOf(
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                )
            )
        ) {

            LitePal.getDatabase()

            CldNaviManager.getInstance().init(this, object : CldMap.NaviInitListener {
                override fun initFailed(p0: String?) {
                    Log.e("MAP","initFailed $p0")
                }

                override fun initSuccess() {
                    Log.e("MAP","initSuccess")
                }

                override fun initStart() {
                    Log.e("MAP","initStart")
                }

                override fun onAuthResult(state: Int, msg: String?) {
                    if (state == 0) {
                        Log.e("MAP","地图授权成功")
                    } else {
                        Log.e("MAP","地图授权失败")
                    }
                }

            })
            Logger.e("已获取定位权限")
        } else {
            requestPermission(
                arrayOf(
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                ), PERMISS_REQUEST_CODE
            )
        }
        val sha1 = KeyUtil.getSHA1(this)
        Log.e("SHA", "SHA1=====>$sha1")

    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == PERMISS_REQUEST_CODE){

            LitePal.getDatabase()

            CldNaviManager.getInstance().init(this, object : CldMap.NaviInitListener {
                override fun initFailed(p0: String?) {
                    Log.e("MAP","initFailed $p0")

                    if (p0 == "engine appInit fail!(errCode:2)"){
                        runOnUiThread {
                            Toast.makeText(this@SplashActivity, "地图授权激活失败", Toast.LENGTH_LONG).show()
                        }
                        CldTask.postResponseDelayed(null,CldTask.ICldRunResponse {
                            System.exit(0)
                        },3000)
                    }
                }

                override fun initSuccess() {
                    Log.e("MAP","initSuccess")
                }

                override fun initStart() {
                    Log.e("MAP","initStart")
                }

                override fun onAuthResult(state: Int, msg: String?) {
                    if (state == 0) {
                        Log.e("MAP","地图授权成功")
                        runOnUiThread {
                            Toast.makeText(this@SplashActivity, "地图授权成功", Toast.LENGTH_LONG).show()
                        }

                    } else {
                        Log.e("MAP","地图授权失败  state =$state")
                    }
                }

            })
        }
    }

    override fun initStateBarColor() {
        StatusBarUtil.setColor(this, resources.getColor(R.color.white), 0)
    }

    override fun initListener() {
        tv_start.setOnClickListener {
            Intent(this, MainActivity::class.java).apply {
                startActivity(this)
                finish()
            }
        }
    }
}